<?php
/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-11-23
 * Time: 下午4:47
 */

$global_uid = 0;

// 当客户端连上来时分配uid，并保存连接，并通知所有客户端
function handle_connection($connection)
{
    global $websocket, $global_uid;
    // 为这个链接分配一个uid
    $connection->uid = ++$global_uid;
}

// 当客户端发送消息过来时，转发给所有人
function handle_message($connection, $data)
{
    global $websocket;
    foreach($websocket->connections as $conn)
    {
        $conn->send("user[{$connection->uid}] said: $data");
    }
}

// 当客户端断开时，广播给所有客户端
function handle_close($connection)
{
    global $websocket;
    foreach($websocket->connections as $conn)
    {
        $conn->send("user[{$connection->uid}] logout");
    }
}